﻿using System.Data;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System;
using Home.Haushalt.Lib.Element;
using System.Reflection;

namespace Home.Haushalt.Lib
{
    /// <summary>
    /// 
    /// </summary>
    public class CSettings
    {
        #region CONSTANTS


        public const string ATTR_KEY = "key";
        public const string ATTR_VALUE = "value";
        public const string ATTR_NOTE = "note";


        private const string SETTINGNAME_CHANGEBACKUPENABLED = "ChangeBackupEnabled";
        private const string SETTINGNAME_DATARELPATH = "DataRelPath";
        private const string SETTINGNAME_FILERELPATH = "FileRelPath";
        private const string SETTINGNAME_ROWSTATECOLORACTIVE = "RowStateColor.Active";
        private const string SETTINGNAME_ROWSTATECOLORFUTURE = "RowStateColor.Future";
        private const string SETTINGNAME_ROWSTATECOLOROLD = "RowStateColor.Old";
        private const string SETTINGNAME_ROWSTATECOLORBASEMENT = "RowStateColor.Basement";
        private const string SETTINGNAME_ROWSTATECOLORPLUS = "RowStateColor.Plus";
        private const string SETTINGNAME_ROWSTATECOLORMINUS = "RowStateColor.Minus";
        private const string SETTINGNAME_ROWSTATENEWPAGE = "RowStateColor.NewPage";
        private const string SETTINGNAME_MASTERIDENT = "MasterIdent";
        private const string SETTINGNAME_CRYPTEDELEMENTS = "CryptedElements";
        private const string SETTINGNAME_MONTHBALANCEGRIDSALDOALPHA = "MonthbalanceGridSaldoAlpha";
        private const string SETTINGNAME_USER = "User";
        private const string SETTINGNAME_ELEMENTHISTORYCOUNT = "ElementHistoryCount";
        private const string SETTINGNAME_SIDEGRID = "SideGrid";
        private const string SETTINGNAME_SIDEGRIDLAYOUT = "SideGridLayout";

        #endregion CONSTANTS

        #region PROPERTIES

        /// <summary></summary>
        private bool _changeBackupEnabled = true;
        /// <summary></summary>
        public bool ChangeBackupEnabled
        {
            get { _changeBackupEnabled = GetSetting(SETTINGNAME_CHANGEBACKUPENABLED).ToBool(true); return _changeBackupEnabled; }
            set { _changeBackupEnabled = SetSetting(SETTINGNAME_CHANGEBACKUPENABLED, value.ToString()).ToBool(true); }
        }

        /// <summary></summary>
        private int _monthbalanceGridSaldoAlpha = -1;
        /// <summary></summary>
        public int MonthbalanceGridSaldoAlpha
        {
            get { if (_monthbalanceGridSaldoAlpha.Equals(-1))_monthbalanceGridSaldoAlpha = GetSetting(SETTINGNAME_MONTHBALANCEGRIDSALDOALPHA).ToInt(); return _monthbalanceGridSaldoAlpha; }
            //set { _monthbalanceGridSaldoAlpha = SetSetting(SETTINGNAME_MONTHBALANCEGRIDSALDOALPHA, value.ToString()); }
        }

        private int _elementHistoryCount = -1;
        /// <summary></summary>
        public int ElementHistoryCount
        {
            get { if (_elementHistoryCount.Equals(-1))_elementHistoryCount = GetSetting(SETTINGNAME_ELEMENTHISTORYCOUNT).ToInt(); return _elementHistoryCount; }
            //set { _monthbalanceGridSaldoAlpha = SetSetting(SETTINGNAME_MONTHBALANCEGRIDSALDOALPHA, value.ToString()); }
        }

        /// <summary></summary>
        private string _userName;
        /// <summary></summary>
        public string UserName
        {
            get { if (_userName == null)_userName = GetSetting(SETTINGNAME_USER); return _userName; }
            set { _userName = SetSetting(SETTINGNAME_USER, value.ToString()); }
        }

        /// <summary></summary>
        private string _sideGrid;
        /// <summary></summary>
        public string SideGrid
        {
            get { if (_sideGrid == null)_sideGrid = GetSetting(SETTINGNAME_SIDEGRID); return _sideGrid; }
        }

        /// <summary></summary>
        private string _sideGridLayout;
        /// <summary></summary>
        public string SideGridLayout
        {
            get { if (_sideGridLayout == null)_sideGridLayout = GetSetting(SETTINGNAME_SIDEGRIDLAYOUT); return _sideGridLayout; }
        }

        public string StartupPath { get { return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); } }

        /// <summary></summary>
        internal string _dataRelPath;
        /// <summary></summary>
        public string DataRelPath
        {
            get { if (_dataRelPath == null)_dataRelPath = GetSetting(SETTINGNAME_DATARELPATH); return _dataRelPath; }
            set { _dataRelPath = SetSetting(SETTINGNAME_DATARELPATH, value.ToString()); }
        }

        /// <summary></summary>
        private string _dataFullPath;
        /// <summary></summary>
        public string DataFullPath
        {
            get { if (_dataFullPath == null)_dataFullPath = Path.Combine(StartupPath, DataRelPath); return _dataFullPath; }
        }


        /// <summary></summary>
        internal string _fileRelPath;
        /// <summary></summary>
        public string FileRelPath
        {
            get { if (_fileRelPath == null)_fileRelPath = GetSetting(SETTINGNAME_FILERELPATH); return _fileRelPath; }
            set { _fileRelPath = SetSetting(SETTINGNAME_FILERELPATH, value.ToString()); }
        }

        /// <summary></summary>
        private string _fileFullPath;
        /// <summary></summary>
        public string FileFullPath
        {
            get { if (_fileFullPath == null)_fileFullPath = Path.Combine(StartupPath, FileRelPath); return _fileFullPath; }
        }

        /// <summary></summary>
        private List<string> _cryptedElements;
        /// <summary></summary>
        public List<string> CryptedElements
        {
            get
            {
                if (_cryptedElements == null)
                {
                    _cryptedElements = new List<string>();
                    foreach (string cryptedElement in GetSetting(SETTINGNAME_CRYPTEDELEMENTS).Split('|'))
                    {
                        _cryptedElements.Add(cryptedElement);
                    }
                }
                return _cryptedElements;
            }
            //set { _cryptedElements = SetSetting(SETTINGNAME_CRYPTEDELEMENTS, value.ToString()); }
        }

        /// <summary></summary>
        private string _masterIdent;
        /// <summary></summary>
        public string MasterIdent
        {
            get { if (_masterIdent == null)_masterIdent = GetSetting(SETTINGNAME_MASTERIDENT); return _masterIdent; }
            set { _masterIdent = SetSetting(SETTINGNAME_MASTERIDENT, value.ToString()); }
        }

        /// <summary></summary>
        private Color? _rowStateColorActive;
        /// <summary></summary>
        public Color RowStateColorActive
        {
            get { if (!_rowStateColorActive.HasValue)_rowStateColorActive = GetSetting(SETTINGNAME_ROWSTATECOLORACTIVE).ToColor(); return _rowStateColorActive.Value; }
            set { _rowStateColorActive = SetSetting(SETTINGNAME_ROWSTATECOLORACTIVE, value.ToString()).ToColor(); }
        }

        /// <summary></summary>
        private Color? _rowStateColorFuture;
        /// <summary></summary>
        public Color RowStateColorFuture
        {
            get { if (!_rowStateColorFuture.HasValue)_rowStateColorFuture = GetSetting(SETTINGNAME_ROWSTATECOLORFUTURE).ToColor(); return _rowStateColorFuture.Value; }
            set { _rowStateColorFuture = SetSetting(SETTINGNAME_ROWSTATECOLORFUTURE, value.ToString()).ToColor(); }
        }

        /// <summary></summary>
        private Color? _rowStateColorOld;
        /// <summary></summary>
        public Color RowStateColorOld
        {
            get { if (!_rowStateColorOld.HasValue)_rowStateColorOld = GetSetting(SETTINGNAME_ROWSTATECOLOROLD).ToColor(); return _rowStateColorOld.Value; }
            set { _rowStateColorOld = SetSetting(SETTINGNAME_ROWSTATECOLOROLD, value.ToString()).ToColor(); }
        }

        /// <summary></summary>
        private Color? _rowStateColorBasement;
        /// <summary></summary>
        public Color RowStateColorBasement
        {
            get { if (!_rowStateColorBasement.HasValue)_rowStateColorBasement = GetSetting(SETTINGNAME_ROWSTATECOLORBASEMENT).ToColor(); return _rowStateColorBasement.Value; }
            set { _rowStateColorBasement = SetSetting(SETTINGNAME_ROWSTATECOLORBASEMENT, value.ToString()).ToColor(); }
        }

        /// <summary></summary>
        private Color? _rowStateColorPlus;
        /// <summary></summary>
        public Color RowStateColorPlus
        {
            get { if (!_rowStateColorPlus.HasValue)_rowStateColorPlus = GetSetting(SETTINGNAME_ROWSTATECOLORPLUS).ToColor(); return _rowStateColorPlus.Value; }
            set { _rowStateColorPlus = SetSetting(SETTINGNAME_ROWSTATECOLORPLUS, value.ToString()).ToColor(); }
        }

        /// <summary></summary>
        private Color? _rowStateColorMinus;
        /// <summary></summary>
        public Color RowStateColorMinus
        {
            get { if (!_rowStateColorMinus.HasValue)_rowStateColorMinus = GetSetting(SETTINGNAME_ROWSTATECOLORMINUS).ToColor(); return _rowStateColorMinus.Value; }
            set { _rowStateColorMinus = SetSetting(SETTINGNAME_ROWSTATECOLORMINUS, value.ToString()).ToColor(); }
        }

        /// <summary></summary>
        private Color? _rowStateNewPage;
        /// <summary></summary>
        public Color RowStateNewPage
        {
            get { if (!_rowStateNewPage.HasValue)_rowStateNewPage = GetSetting(SETTINGNAME_ROWSTATENEWPAGE).ToColor(); return _rowStateNewPage.Value; }
            set { _rowStateNewPage = SetSetting(SETTINGNAME_ROWSTATENEWPAGE, value.ToString()).ToColor(); }
        }

        #endregion PROPERTIES

        /// <summary></summary>
        private DataTable _settingsTable;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="settingsTable"></param>
        public CSettings(DataTable settingsTable)
        {
            _settingsTable = settingsTable;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="settingKey"></param>
        /// <returns></returns>
        private string GetSetting(string settingKey)
        {
            if (_settingsTable != null)
            {
                DataRow[] rows = _settingsTable.Select(string.Format("{0} = '{1}'", ATTR_KEY, settingKey));
                if (rows.Length > 0)
                {
                    return rows[0][ATTR_VALUE].ToString();
                }
            }

            return "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="settingKey"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        private string SetSetting(string settingKey, string newValue)
        {
            DataRow[] rows = _settingsTable.Select(string.Format("{0} = '{1}'", ATTR_KEY, settingKey));
            if (rows.Length > 0)
            {
                rows[0][ATTR_VALUE] = newValue;
                //SaveSettings();
            }
            return newValue;
        }
    }
}
